<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>宿舍管理系统</title>
    <meta name="renderer" content="webkit">
    <meta name="csrf-token" content="{{ csrf_token() }}" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />

    <link rel="stylesheet" href="{{ asset('css/font.css') }}">
    <link rel="stylesheet" href="{{ asset('css/xadmin.css') }}">
    <script type="text/javascript" src="{{ asset('lib/layui/layui.js') }}" charset="utf-8"></script>
    <script type="text/javascript" src="{{ asset('js/xadmin.js') }}"></script>

    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                {{ csrf_field() }}
                <div class="layui-form-item">
                    <label class="layui-form-label" for="category_id">
                        <span class="x-red">*</span>所属类别
                    </label>
                    <div class="layui-input-inline">
                        <select name="category_id" lay-verify="required">
                            @foreach ($categories as $k => $v)
                                <option value="{{ $k }}">
                                    {{ $v }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="subitem_name" class="layui-form-label">
                        <span class="x-red">*</span>具体评分项目名
                    </label>
                    <div class="layui-input-inline">
                        <textarea class="layui-textarea" id="subitem_name" name="subitem_name" required="" lay-verify="required"
                            autocomplete="off"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="subitem_score" class="layui-form-label">
                        <span class="x-red">*</span>该具体项目分值
                    </label>
                    <div class="layui-input-inline">
                        <input type="number" name="subitem_score" id="subitem_score" lay-verify="number"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="default_score" class="layui-form-label">
                        <span class="x-red">*</span>该具体项目默认分值
                    </label>
                    <div class="layui-input-inline">
                        <input type="number" name="default_score" id="default_score" lay-verify="number"
                            class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="column_width" class="layui-form-label">
                        <span class="x-red">*</span>该具体项目列宽
                    </label>
                    <div class="layui-input-inline">
                        <input type="number" name="column_width" id="column_width" lay-verify="number"
                            class="layui-input">
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script>
        layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                    layer = layui.layer;

                // 计算最长标签的宽度
                var maxWidth = 120;
                $('.layui-form-label').each(function() {
                    maxWidth = Math.max(maxWidth, $(this).width());
                });

                // 统一设置标签宽度
                $('.layui-form-label').css('width', maxWidth + 20 + 'px'); // 增加20px的边距
                //监听提交
                form.on('submit(add)',
                    function(data) {
                        console.log(data.field);
                        //发异步删除数据
                        $.ajaxSetup({
                            headers: {
                                'X-CSRF-Token': $('input[name="_token"]').val()
                            }
                        });
                        let url = '/rating-subitems';
                        $.ajax({
                            url: url,
                            data: data.field,
                            type: 'POST',
                            dataType: "json",
                            success: function() {
                                console.log('success')
                                layer.alert('添加成功', {
                                    icon: 6,
                                    time: 5000
                                }, function() {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口数据表进行刷新
                                    parent.layui.table.reload('data-table');
                                });
                            },
                            error: function(err) {
                                console.log(err);
                                layer.msg('保存失败', {
                                    icon: 2,
                                    time: 1000
                                });

                            }
                        });
                        //ajax 异步调用会发生再这个语句之后，返回false可以避免异步返回时页面发生了刷新
                        return false;
                    });

            });
    </script>
</body>

</html>
